package org.jsola.hr.service.impl;

import org.jsola.common.DateKit8;
import org.jsola.core.Page;
import org.jsola.exception.ParamException;
import org.jsola.hr.common.DateUtils;
import org.jsola.hr.common.ExampleKit;
import org.jsola.hr.constant.HrConstants;
import org.jsola.hr.dao.IEmpSocMonthDAO;
import org.jsola.hr.dto.EmpSocMonthAddDTO;
import org.jsola.hr.dto.EmpSocMonthUpdateDTO;
import org.jsola.hr.entity.EmpInfoDO;
import org.jsola.hr.entity.EmpSocInfoDO;
import org.jsola.hr.entity.EmpSocMonthDO;
import org.jsola.hr.entity.SocCheckDO;
import org.jsola.hr.query.EmpSocInfoQuery;
import org.jsola.hr.query.EmpSocMonthQuery;
import org.jsola.hr.query.SocCheckCustomQuery;
import org.jsola.hr.service.*;
import org.jsola.hr.vo.*;
import org.jsola.salary.common.StringKit;
import org.jsola.user.core.TokenUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.*;
import java.util.stream.Collectors;

import static org.jsola.hr.constant.EmpSocStatus.NOT_SOC;

/**
 * 员工参保信息月统计
 *
 * @author zhr
 */
@Service("hrEmpSocMonthServiceImpl")
public class EmpSocMonthServiceImpl implements IEmpSocMonthService {

    @Autowired
    private IEmpSocMonthDAO empSocMonthDAO;

    @Autowired
    private IEmpSocInfoService empSocInfoService;

    @Autowired
    private ICostBillService costBillService;

    @Autowired
    private IEmpSocItemService empSocItemService;

    @Autowired
    private IEmpInfoService empInfoService;

    @Autowired
    private ISocCheckService socCheckService;

    @Autowired
    private IEmpSocTransformService empSocTransformService;

    @Override
    @Transactional(value = HrConstants.TX, rollbackFor = Exception.class)
    public EmpSocMonthVO save(EmpSocMonthAddDTO empSocMonthAddDTO, TokenUser tokenUser) {
        boolean existsByYearAndMonth = existsByYearAndMonth(empSocMonthAddDTO.getCompanyId()
                , empSocMonthAddDTO.getYear(), empSocMonthAddDTO.getMonth(), tokenUser.getSiteId());
        ParamException.isFalse(existsByYearAndMonth, "该用工单位本月已创建参保");
        // 转DO
        EmpSocMonthDO empSocMonthDO = empSocMonthAddDTO.to(EmpSocMonthDO.class);
        // 保存
        empSocMonthDO = save(empSocMonthDO, tokenUser.getSiteId(), tokenUser.getUserId());
        return empSocMonthDO.to(EmpSocMonthVO.class);
    }

    @Override
    @Transactional(value = HrConstants.TX, rollbackFor = Exception.class)
    public EmpSocMonthVO initSave(EmpSocMonthAddDTO empSocMonthAddDTO, TokenUser tokenUser) {
        boolean existsByYearAndMonth = existsByYearAndMonth(empSocMonthAddDTO.getCompanyId()
                , empSocMonthAddDTO.getYear(), empSocMonthAddDTO.getMonth(), tokenUser.getSiteId());
        ParamException.isFalse(existsByYearAndMonth, "该用工单位本月已创建参保");
        // 转DO
        EmpSocMonthDO empSocMonthDO = empSocMonthAddDTO.to(EmpSocMonthDO.class);
        // 保存
        empSocMonthDO = save(empSocMonthDO, tokenUser.getSiteId(), tokenUser.getUserId());
        empSocInfoService.updateMonthId(empSocMonthDO, tokenUser);
        return empSocMonthDO.to(EmpSocMonthVO.class);
    }

    @Override
    @Transactional(value = HrConstants.TX, rollbackFor = Exception.class)
    public int update(EmpSocMonthUpdateDTO empSocMonthUpdateDTO, TokenUser tokenUser) {
        //转DO
        EmpSocMonthDO empSocMonthDO = empSocMonthUpdateDTO.to(EmpSocMonthDO.class);
        //根据主键更新，只更新非null值
        return updateByIdSelective(empSocMonthDO, tokenUser.getSiteId(), tokenUser.getUserId());
    }

    @Override
    @Transactional(value = HrConstants.TX, rollbackFor = Exception.class)
    public int deleteByIds(TokenUser tokenUser, String... empSocMonthIds) {
        // 查出参保月的所有参保信息
        List<EmpSocInfoDO> empSocInfoList = empSocInfoService.selectByMonthIds(empSocMonthIds);
        List<String> empSocInfoIdList = empSocInfoList.stream().map(EmpSocInfoDO::getId).collect(Collectors.toList());
        // 删除参保信息
        empSocInfoService.updateEmpSocInfoByMonthId(empSocMonthIds, tokenUser);
        // 删除参保信息子项
        if (!CollectionUtils.isEmpty(empSocInfoIdList)) {
            empSocItemService.updateEmpSocItemByEmpSocInfoIds(empSocInfoIdList, tokenUser);
        }
        List<String> empId = empSocInfoList.stream().map(EmpSocInfoDO::getEmpId).distinct().collect(Collectors.toList());
        List<EmpInfoDO> empInfoList = empInfoService.selectDoByIds(empId);
        // 删除这个参保月之后如果这一批人没有参保记录了就把他们赋值为未参保
        for (EmpInfoDO empInfoDO : empInfoList) {
            List<EmpSocInfoDO> empSocInfoS = empSocInfoService.selectByEmpId(empInfoDO.getId(), tokenUser);
            if (CollectionUtils.isEmpty(empSocInfoS)) {
                empInfoDO.setEmpSocStatus(NOT_SOC.getValue());
                empInfoService.updateByIdSelective(empInfoDO, tokenUser.getSiteId(), tokenUser.getUserId());
            }
        }
        // 删除参保月的同时删除此月校验信息
        List<String> monthIdList = Arrays.asList(empSocMonthIds);
        socCheckService.deleteByMonthIdList(monthIdList, tokenUser);
        return logicalDeleteByIds(tokenUser.getSiteId(), tokenUser.getUserId(), (Object[]) empSocMonthIds);
    }

    @Override
    @Transactional(value = HrConstants.TX, rollbackFor = Exception.class)
    public int deleteByCompanyIdAndYearMonth(String companyId, String year, String month, TokenUser tokenUser) {
        String monthId = selectMonthIdByYearAndMonth(companyId, year, month, tokenUser.getSiteId());
        if (StringKit.isBlank(monthId)) {
            throw new ParamException("参保月不存在");
        }
        List<EmpSocInfoDO> empSocInfoList = empSocInfoService.selectEmpInfoByMonthId(companyId, monthId, tokenUser);
        if (!CollectionUtils.isEmpty(empSocInfoList)) {
            throw new ParamException("参保月中还有未操作的参保人员");
        }
        return logicalDeleteByIds(tokenUser.getSiteId(), tokenUser.getUserId(), monthId);
    }


    @Override
    public EmpSocMonthVO selectById(String empSocMonthId, String siteId) {
        EmpSocMonthDO empSocMonthDO = selectDOById(empSocMonthId, siteId);
        if (empSocMonthDO == null) {
            return null;
        }
        return empSocMonthDO.to(EmpSocMonthVO.class);
    }

    @Override
    public List<EmpSocMonthListVO> select(EmpSocMonthQuery empSocMonthQuery, String siteId) {
        List<EmpSocMonthDO> empSocMonthDOList = selectDO(empSocMonthQuery, siteId);
        if (CollectionUtils.isEmpty(empSocMonthDOList)) {
            return empSocMonthDOList == null ? null : new ArrayList<>();
        }
        return empSocMonthDOList.stream()
                .map(empSocMonthDO -> empSocMonthDO.to(EmpSocMonthListVO.class))
                .collect(Collectors.toList());
    }

    @Override
    public int selectCount(EmpSocMonthQuery empSocMonthQuery, String siteId) {
        Example example = buildExample(empSocMonthQuery, siteId);
        return empSocMonthDAO.selectCountByExample(example);
    }

    @Override
    public Page<EmpSocMonthListVO> selectPage(EmpSocMonthQuery empSocMonthQuery, String siteId) {
        Example example = buildExample(empSocMonthQuery, siteId);
        Page<EmpSocMonthDO> page = empSocMonthDAO.selectPageByExample(example,
                empSocMonthQuery.getPageNo(),
                empSocMonthQuery.getPageSize());

        return page.to(EmpSocMonthListVO.class);
    }

    @Override
    public EmpSocMonthDO selectDOById(String empSocMonthId, String siteId) {
        return listById(empSocMonthId, siteId);
    }

    @Override
    public List<EmpSocMonthDO> selectDO(EmpSocMonthQuery empSocMonthQuery, String siteId) {
        Example example = buildExample(empSocMonthQuery, siteId);
        return empSocMonthDAO.selectByExample(example);
    }

    @Override
    public List<YearAndMonthVO> listYearAndMonthByCompany(String companyId, String siteId) {
        return empSocMonthDAO.listYearAndMonthByCompany(companyId, siteId);
    }

    @Override
    public boolean existsByYearAndMonth(String companyId, String year, String month, String siteId) {
        Example example = new Example(EmpSocMonthDO.class);
        example.and()
                .andEqualTo("companyId", companyId)
                .andEqualTo("year", year)
                .andEqualTo("month", month)
                .andEqualTo("valid", true)
                .andEqualTo("siteId", siteId);
        int count = empSocMonthDAO.selectCountByExample(example);
        return count > 0;
    }

    @Override
    public String selectMonthIdByCurrentDate(String companyId, Date date, String siteId) {
        String[] yearAndMonth = DateUtils.getYearAndMonth(date);
        Example example = new Example(EmpSocMonthDO.class);
        example.and()
                .andEqualTo("companyId", companyId)
                .andEqualTo("year", yearAndMonth[0])
                .andEqualTo("month", yearAndMonth[1])
                .andEqualTo("valid", true)
                .andEqualTo("siteId", siteId);
        EmpSocMonthDO empSocMonthDO = empSocMonthDAO.selectOneByExample(example);
        if (ObjectUtils.isEmpty(empSocMonthDO)) {
            return "";
        } else {
            return empSocMonthDO.getId();
        }
    }

    @Override
    public String selectMonthIdByYearAndMonth(String companyId, String year, String month, String siteId) {
        Example example = new Example(EmpSocMonthDO.class);
        example.and()
                .andEqualTo("companyId", companyId)
                .andEqualTo("year", year)
                .andEqualTo("month", month)
                .andEqualTo("valid", true)
                .andEqualTo("siteId", siteId);
        EmpSocMonthDO empSocMonthDO = empSocMonthDAO.selectOneByExample(example);
        if (ObjectUtils.isEmpty(empSocMonthDO)) {
            return "";
        } else {
            return empSocMonthDO.getId();
        }
    }

    @Override
    public List<EmpSocMonthDO> selectByYearAndMonth(List<String> companyIdList, String year, String month, String siteId) {
        Example example = new Example(EmpSocMonthDO.class);
        example.and()
                .andIn("companyId", companyIdList)
                .andEqualTo("year", year)
                .andEqualTo("month", month)
                .andEqualTo("valid", true)
                .andEqualTo("siteId", siteId);
        return empSocMonthDAO.selectByExample(example);

    }

    @Override
    public EmpSocMonthDO selectMonthByYearAndMonth(String companyId, String year, String month, String siteId) {
        Example example = new Example(EmpSocMonthDO.class);
        example.and()
                .andEqualTo("companyId", companyId)
                .andEqualTo("year", year)
                .andEqualTo("month", month)
                .andEqualTo("valid", true)
                .andEqualTo("siteId", siteId);
        EmpSocMonthDO empSocMonthDO = empSocMonthDAO.selectOneByExample(example);
        if (ObjectUtils.isEmpty(empSocMonthDO)) {
            return null;
        } else {
            return empSocMonthDO;
        }
    }

    @Override
    public List<String> selectMonthIdByYearAndMonthCompanyIdList(List<String> companyIdList, String year, String month, String siteId) {
        Example example = new Example(EmpSocMonthDO.class);
        example.and()
                .andIn("companyId", companyIdList)
                .andEqualTo("year", year)
                .andEqualTo("month", month)
                .andEqualTo("valid", true)
                .andEqualTo("siteId", siteId);
        List<EmpSocMonthDO> empSocMonthList = empSocMonthDAO.selectByExample(example);
        if (CollectionUtils.isEmpty(empSocMonthList)) {
            return new ArrayList<>();
        } else {
            return empSocMonthList.stream().map(EmpSocMonthDO::getId).collect(Collectors.toList());
        }
    }

    @Override
    public Map<String, Date> selectMonthIdGreatThanYearAndMonth(String companyId, String siteId) {
        Map<String, Date> result = new HashMap<>(8);
        String[] yearAndMonth = DateUtils.getYearAndMonth(new Date());
        Example example = new Example(EmpSocMonthDO.class);
        example.and()
                .andEqualTo("companyId", companyId)
                .andEqualTo("valid", true)
                .andCondition("concat(year,month) > " + yearAndMonth[0] + yearAndMonth[1])
                .andEqualTo("siteId", siteId);
        List<EmpSocMonthDO> empSocMonthList = empSocMonthDAO.selectByExample(example);
        if (!CollectionUtils.isEmpty(empSocMonthList)) {
            for (EmpSocMonthDO empSocMonthDO : empSocMonthList) {
                result.put(empSocMonthDO.getId(), DateKit8.parse(empSocMonthDO.getYear() + "-" + empSocMonthDO.getMonth() + "-01", "yyyy-MM-dd"));
            }
        }
        return result;
    }

    @Override
    public String selectMonthIdByDate(String companyId, Date date, String siteId) {
        String[] yearAndMonth = DateUtils.getYearAndMonth(date);
        Example example = new Example(EmpSocMonthDO.class);
        example.and()
                .andEqualTo("companyId", companyId)
                .andEqualTo("year", yearAndMonth[0])
                .andEqualTo("month", yearAndMonth[1])
                .andEqualTo("valid", true)
                .andEqualTo("siteId", siteId);
        EmpSocMonthDO empSocMonthDO = empSocMonthDAO.selectOneByExample(example);
        if (ObjectUtils.isEmpty(empSocMonthDO)) {
            return "";
        } else {
            return empSocMonthDO.getId();
        }
    }

    @Override
    public List<String> selectMonthIdByDateNotSite(Date date) {
        String[] yearAndMonth = DateUtils.getYearAndMonth(date);
        Example example = new Example(EmpSocMonthDO.class);
        example.and()
                .andEqualTo("year", yearAndMonth[0])
                .andEqualTo("month", yearAndMonth[1])
                .andEqualTo("valid", true);
        List<EmpSocMonthDO> empSocMonthDOList = empSocMonthDAO.selectByExample(example);
        if (CollectionUtils.isEmpty(empSocMonthDOList)) {
            return new ArrayList<>();
        } else {
            return empSocMonthDOList.parallelStream().map(EmpSocMonthDO::getId).collect(Collectors.toList());
        }
    }

    @Override
    public List<String> selectMonthIdForWhile(String startYear, String startMonth, String endYear,
                                              String endMonth, String companyId, Boolean isAcrossYear, String siteId) {
        List<EmpSocMonthVO> empSocMonthList = empSocMonthDAO.selectMonthIdForWhile(startYear, startMonth, endYear,
                endMonth, companyId, isAcrossYear, siteId);
        return empSocMonthList.stream().map(EmpSocMonthVO::getId).collect(Collectors.toList());
    }

    @Override
    public List<String> selectOneYearMonthId(String year, String companyId, String siteId) {
        List<EmpSocMonthVO> empSocMonthList = empSocMonthDAO.selectOneYearMonthId(year, companyId, siteId);
        return empSocMonthList.stream().map(EmpSocMonthVO::getId).collect(Collectors.toList());
    }

    @Override
    @Transactional(value = HrConstants.TX, rollbackFor = Exception.class)
    public EmpSocMonthVO saveByDate(String companyId, Date date, TokenUser tokenUser) {
        String[] yearAndMonth = DateUtils.getYearAndMonth(date);
        EmpSocMonthAddDTO empSocMonthAddDTO = new EmpSocMonthAddDTO();
        empSocMonthAddDTO.setCompanyId(companyId);
        empSocMonthAddDTO.setYear(yearAndMonth[0]);
        empSocMonthAddDTO.setMonth(yearAndMonth[1]);
        return save(empSocMonthAddDTO, tokenUser);
    }

    @Override
    @Transactional(value = HrConstants.TX, rollbackFor = Exception.class)
    public EmpSocMonthVO saveByYearAndMonth(String companyId, String year, String month, TokenUser tokenUser) {
        EmpSocMonthAddDTO empSocMonthAddDTO = new EmpSocMonthAddDTO();
        empSocMonthAddDTO.setCompanyId(companyId);
        empSocMonthAddDTO.setYear(year);
        empSocMonthAddDTO.setMonth(month);
        return save(empSocMonthAddDTO, tokenUser);
    }

    @Override
    public List<EmpSocMonthDO> selectHomeMonthId(String year, String month, List<String> companyIdList, TokenUser tokenUser) {
        Example example = new Example(EmpSocMonthDO.class);
        example.and()
                .andEqualTo("valid", true)
                .andEqualTo("siteId", tokenUser.getSiteId())
                .andEqualTo("year", year)
                .andEqualTo("month", month);
        if (!CollectionUtils.isEmpty(companyIdList)) {
            example.and().andIn("companyId", companyIdList);
        }
        return empSocMonthDAO.selectByExample(example);
    }

    @Override
    public MonthNextAndPreviousCheckVO checkMonthNextAndPrevious(String year, String month, String companyId, String siteId) {
        EmpSocMonthDO empSocMonthDO = checkLastMonth(companyId, siteId);
        MonthNextAndPreviousCheckVO monthNextAndPreviousCheckVO = new MonthNextAndPreviousCheckVO();
        // 当前月
        Date currentDate = DateKit8.parse(year + "-" + month + "-01", "yyyy-MM-dd");
        // 上月下月
        Date datePrevious = DateKit8.addMonths(currentDate, -1L);
        Date dateNext = DateKit8.addMonths(currentDate, 1L);
        // 获取上月下月当月的monthId
        String monthIdPrevious = selectMonthIdByDate(companyId, datePrevious, siteId);
        if (!StringUtils.isEmpty(monthIdPrevious)) {
            monthNextAndPreviousCheckVO
                    .setMonthPreviousCanNotEdited(costBillService.checkMonthIdCreateCostBill(monthIdPrevious, siteId));
        }
        String monthIdNext = selectMonthIdByDate(companyId, dateNext, siteId);
        if (!StringUtils.isEmpty(monthIdNext)) {
            monthNextAndPreviousCheckVO
                    .setMonthNextCanNotEdited(costBillService.checkMonthIdCreateCostBill(monthIdNext, siteId));
        }
        String monthIdCurrent = selectMonthIdByDate(companyId, currentDate, siteId);
        if (!StringUtils.isEmpty(monthIdCurrent)) {
            monthNextAndPreviousCheckVO
                    .setMonthCurrentCanNotEdited(costBillService.checkMonthIdCreateCostBill(monthIdCurrent, siteId));
        }
        if (!ObjectUtils.isEmpty(empSocMonthDO)) {
            monthNextAndPreviousCheckVO.
                    setMonthWhetherCurrent(empSocMonthDO.getYear().equals(year) && empSocMonthDO.getMonth().equals(month));
        } else {
            monthNextAndPreviousCheckVO.setMonthWhetherCurrent(false);
        }
        monthNextAndPreviousCheckVO.setMonthIdPrevious(monthIdPrevious);
        monthNextAndPreviousCheckVO.setMonthIdNext(monthIdNext);
        monthNextAndPreviousCheckVO.setMonthCurrent(monthIdCurrent);
        return monthNextAndPreviousCheckVO;
    }

    @Override
    public List<String> selectCurrentDateAfterMonthId(String startDate, String endDate, String companyId, String siteId) {
        List<String> monthIdList = empSocMonthDAO.selectCurrentDateBetweenMonthId(startDate, endDate, companyId, siteId);
        if (CollectionUtils.isEmpty(monthIdList)) {
            return new ArrayList<>();
        }
        return monthIdList;
    }

    @Override
    public EmpSocMonthDO checkLastMonth(String companyId, String siteId) {
        Example example = new Example(EmpSocMonthDO.class);
        example.and()
                .andEqualTo("siteId", siteId)
                .andEqualTo("companyId", companyId);
        example.orderBy("year").desc().orderBy("month").desc();
        List<EmpSocMonthDO> empSocMonthList = empSocMonthDAO.selectByExample(example);
        if (CollectionUtils.isEmpty(empSocMonthList)) {
            return null;
        }
        return empSocMonthList.get(0);
    }

    @Override
    public EmpSocMonthDO checkLastMonthValid(String companyId, String siteId) {
        Example example = new Example(EmpSocMonthDO.class);
        example.and()
                .andEqualTo("siteId", siteId)
                .andEqualTo("valid", true)
                .andEqualTo("companyId", companyId);
        example.orderBy("year").desc().orderBy("month").desc();
        List<EmpSocMonthDO> empSocMonthList = empSocMonthDAO.selectByExample(example);
        if (CollectionUtils.isEmpty(empSocMonthList)) {
            return null;
        }
        return empSocMonthList.get(0);
    }

    @Override
    public List<EmpSocMonthListVO> listAllByCompanyId(SocCheckCustomQuery socCheckCustomQuery, TokenUser tokenUser) {
        Example example = new Example(EmpSocMonthDO.class);
        example.and()
                .andEqualTo("valid", true)
                .andEqualTo("siteId", tokenUser.getSiteId())
                .andEqualTo("year", socCheckCustomQuery.getYear())
                .andEqualTo("companyId", socCheckCustomQuery.getCompanyId());
        if (!StringUtils.isEmpty(socCheckCustomQuery.getMonth())) {
            example.and().andEqualTo("month", socCheckCustomQuery.getMonth());
        }
        List<EmpSocMonthDO> empSocMonthList = empSocMonthDAO.selectByExample(example);
        return empSocMonthList.stream().map(empSocMonthDO -> {
            EmpSocMonthListVO empSocMonthListVO = empSocMonthDO.to(EmpSocMonthListVO.class);
            EmpSocInfoQuery empSocInfoQuery = new EmpSocInfoQuery();
            empSocInfoQuery.setYear(empSocMonthDO.getYear());
            empSocInfoQuery.setMonth(empSocMonthDO.getMonth());
            empSocInfoQuery.setCompanyId(socCheckCustomQuery.getCompanyId());
            empSocMonthListVO.setCheckCount(socCheckService
                    .selectCountByMonthId(socCheckCustomQuery.getCheckType(), empSocMonthDO.getId(), tokenUser));
            EmpSocTransformCountVO countVO =
                    empSocTransformService.selectEmpSocCount(empSocInfoQuery, tokenUser);
            // 参保人数
            empSocMonthListVO.setEmpCount(countVO.getAllCount());
            // 社保缴费
            empSocMonthListVO.setSocAmount(countVO.getCompanySocAmount().add(countVO.getPersonalSocAmount()));
            // 公积金缴费
            empSocMonthListVO.setPfAmount(countVO.getCompanyPfAmount().add(countVO.getPersonalPfAmount()));
            List<SocCheckDO> socCheckList = socCheckService.selectByMonthId(empSocMonthDO.getId(), tokenUser);
            if (!CollectionUtils.isEmpty(socCheckList)) {
                empSocMonthListVO.setFileUrl(socCheckList.get(0).getFileUrl());
                empSocMonthListVO.setFileName(socCheckList.get(0).getFileName());
            }
            return empSocMonthListVO;
        }).collect(Collectors.toList());

    }

    @Override
    public List<EmpSocMonthDO> selectByMonthIdList(List<String> monthIdList) {
        Example example = new Example(EmpSocMonthDO.class);
        example.and()
                .andEqualTo("valid", true)
                .andIn("id", monthIdList);
        return empSocMonthDAO.selectByExample(example);
    }

    @Override
    public List<String> selectMonthIdListAfterAndEqualsDate(String companyId, String dateStr, String siteId) {
        return empSocMonthDAO.selectMonthIdListAfterAndEqualsDate(dateStr, companyId, siteId);
    }

    @Override
    public List<EmpSocMonthListVO> selectByMonthByDateStrList(String companyId, List<String> dateStr, String siteId) {
        return empSocMonthDAO.selectByMonthByDateStrList(companyId, dateStr, siteId);
    }

    /**
     * 根据查询参数，构建example
     *
     * @param empSocMonthQuery 查询参数
     * @param siteId           所属站点id
     * @return example
     */
    private Example buildExample(EmpSocMonthQuery empSocMonthQuery, String siteId) {
        Example example = new Example(EmpSocMonthDO.class);
        example.and()
                .andEqualTo("valid", true)
                .andEqualTo("siteId", siteId);
        if (empSocMonthQuery.getYear() != null) {
            example.and().andEqualTo("year", empSocMonthQuery.getYear());
        }
        if (empSocMonthQuery.getMonth() != null) {
            example.and().andEqualTo("month", empSocMonthQuery.getMonth());
        }
        if (empSocMonthQuery.getCompanyId() != null) {
            example.and().andEqualTo("companyId", empSocMonthQuery.getCompanyId());
        }
        // 排序
        ExampleKit.setExampleOrder(example, empSocMonthQuery.getOrders());
        return example;
    }
}




